/*
	four sided obstacle
	notes: making a bunch of obstacles, defined as a map, is not a big deal.
		however, the bounce doesn't work in corners because the ball intersects with
		a horizontal and vertical side at the same time.
		garbage.
*/

#include <hge.h>
#include <hgesprite.h>
#include <hgevector.h>
#include "obstacle.h"
//#include "ball.h"

Obstacle::Obstacle()
{
	x=200.0f; y=200.0f;
	w=130.0f; h=130.0f;
	Obstacle::ObstacleInit();
	winner=false;
}

Obstacle::Obstacle(float X, float Y, float W, float H, bool win)
{
	x=16*X; y=16*Y;
	w=16*W+2; h=16*H+2;
	winner=win;
	Obstacle::ObstacleInit();
}

Obstacle::~Obstacle()
{
	for(int i=0;i<4;i++){
		delete side[i];
	}
	delete box;
	delete spr;
}

void Obstacle::ObstacleInit()
{
	spr=new hgeSprite(obtex,x,y,w,h);
	spr->SetTextureRect(0,0,w,h);
	spr->SetBlendMode(BLEND_COLORMUL | BLEND_ALPHABLEND | BLEND_NOZWRITE);
	if (winner)
		spr->SetColor(0xFFFF8000);
	else
		spr->SetColor(0xFFCCFFFF);

	box=new hgeRect(x-16,y-16,x+w+2*16,y+h+2*16);//to catch the ball before it actually colides.

	side[0]=new hgeRect(x,y,x+2,y+h);//left
	/*dSpr[0]=new hgeSprite(obtex,x,y,2,h);
	dSpr[0]->SetBlendMode(BLEND_COLORADD | BLEND_ALPHABLEND | BLEND_NOZWRITE);
	dSpr[0]->SetColor(0xAAFF4444);*/

	side[1]=new hgeRect(x,y,x+w,y+2);//top
	/*dSpr[1]=new hgeSprite(obtex,x,y,w,2);
	dSpr[1]->SetBlendMode(BLEND_COLORADD | BLEND_ALPHABLEND | BLEND_NOZWRITE);
	dSpr[1]->SetColor(0xAAFF4444);*/

	side[2]=new hgeRect(x+w-2,y,x+w,y+h);//right side
	/*dSpr[2]=new hgeSprite(obtex,x+w-2,y,2,h);
	dSpr[2]->SetBlendMode(BLEND_COLORADD | BLEND_ALPHABLEND | BLEND_NOZWRITE);
	dSpr[2]->SetColor(0xAAFF4444);*/

	side[3]=new hgeRect(x,y+h-2,x+w,y+h);//bottom
	/*dSpr[3]=new hgeSprite(obtex,x,y+h-2,w,2);
	dSpr[3]->SetBlendMode(BLEND_COLORADD | BLEND_ALPHABLEND | BLEND_NOZWRITE);
	dSpr[3]->SetColor(0xAAFF4444);*/
}

int Obstacle::Collide(Ball *ball)
{	
	//hgeRect *ballBox=ball->Box();
	/*hgeVector ballTravel=ball->travel;
	ballTravel.Normalize();*/
	

	/*if (box->Intersect(ballBox) && winner == true){
		return 4; //we don't really care what direction the ball should be going in if the person wins!
	}*/

	//if (box->TestPoint(ball->x+ballTravel.x*16,ball->y+ballTravel.y*16)) //meaningless
	if (box->TestPoint(ball->x,ball->y)){
		//if (winner) return 4;
		return ball->Bounce(this);
		/*
		if (side[0]->Intersect(ballBox)) return 0;
		if (side[1]->Intersect(ballBox)) return 1;
		if (side[2]->Intersect(ballBox)) return 2;
		if (side[3]->Intersect(ballBox)) return 3;
		*/
		
	}
	return 0;
}

//the return values for the unused component in a collision is basically 
//irrelevant...
float Obstacle::SideX(int which)
{
	switch(which){
	case 0: return x;
	case 1:	return 0;
	case 2: return x+w;
	case 3: return 0;
	default: return -1;
	}
}

float Obstacle::SideY(int which)
{
	switch(which){
	case 0: return 0;
	case 1:	return y;
	case 2: return 0;
	case 3: return y+h;
	default: return -1;
	}
}

void Obstacle::Render()
{
	spr->Render(x,y);
	/*dSpr[0]->Render(x,y); //de-buggery
	dSpr[1]->Render(x,y);
	dSpr[2]->Render(x+w-2,y);
	dSpr[3]->Render(x,y+h);*/
}